package mwa.cs.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;

/**
 * A class used for managing menu
 */
public class MenuDAO {

	public List<Map<String, Object>> getMenu(int poUserID) {
		String vaSql = "select mn.MENU_ID, mn.MENU_NAME , mn.MENU_URL , mn.MENU_PARENT_ID , mn.MENU_PARENT , mn.MENU_SEQ , mn.MENU_ROUTE_PREVIOUS_ID , mn.MENU_ROUTE_ID , mn.MENU_ROUTE_PARENT_ID , mn.INSERT_USER_ID , mn.INSERT_DATE , mn.INSERT_TIME , mn.UPDATE_USER_ID , mn.UPDATE_DATE , mn.UPDATE_TIME "
				+ "from USER_LOGIN ul join SM_USER_ROLE ur on ul.user_id = ur.user_id "
				+ "join SITES s on ul.site_id = s.site_id "
				+ "join SM_ROLE r on ur.role_id = r.role_id and s.site_id = r.site_id "
				+ "join SM_PACKAGE pk on s.package_id = pk.package_id "
				+ "join SM_MAPPING m on pk.package_id = m.package_id and r.role_id = m.role_id "
				+ "join SM_MENU mn on m.menu_id = mn.menu_id "
				+ "where ul.user_id = ? " + "order by mn.MENU_SEQ";

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		try {
			Database db = new Database();
			result = db.queryList(vaSql, poUserID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public HashMap<String, Object> getMenuActive(String poMenuURL,
			String poMenuID , String poSrvlName) {
		String[] vaChkMainpage = poMenuURL.split("/CS");
		String vaMainPageName = "";
		if (vaChkMainpage.length > 1) {
			vaMainPageName = vaChkMainpage[0]
					+ "/CS"
					+ (Integer.parseInt(vaChkMainpage[1].replace(".jsp", "")) - 1)
					+ ".jsp";
		}

		String vaSql = "";
		Database db = new Database();
		HashMap<String, Object> result = null;
		if (poMenuID.equals("") || poMenuID.equals(null)) {
			vaSql = "select m1.MENU_ROUTE_ID AS MAIN_ACIVE,m1.MENU_ROUTE_PARENT_ID AS PARENT_ACTIVE1,m2.MENU_ROUTE_PARENT_ID AS PARENT_ACTIVE2 "
					+ "from SM_MENU m1 left join SM_MENU m2 on m1.MENU_ROUTE_PARENT_ID = m2.MENU_ROUTE_ID "
					+ "where m1.MENU_URL = ? OR m1.MENU_URL = ?";
			result = (HashMap<String, Object>) db.querySingle(vaSql, poMenuURL,poSrvlName);
		} else {
			vaSql = "select m1.MENU_ROUTE_ID AS MAIN_ACIVE,m1.MENU_ROUTE_PARENT_ID AS PARENT_ACTIVE1,m2.MENU_ROUTE_PARENT_ID AS PARENT_ACTIVE2 "
					+ "from SM_MENU m1 left join SM_MENU m2 on m1.MENU_ROUTE_PARENT_ID = m2.MENU_ROUTE_ID "
					+ "where m1.MENU_ID = ?";
			result = (HashMap<String, Object>) db.querySingle(vaSql, poMenuID);
		}

		if (result == null) {
			vaSql = "select m1.MENU_ROUTE_ID AS MAIN_ACIVE,m1.MENU_ROUTE_PARENT_ID AS PARENT_ACTIVE1,m2.MENU_ROUTE_PARENT_ID AS PARENT_ACTIVE2 "
					+ "from SM_MENU m1 left join SM_MENU m2 on m1.MENU_ROUTE_PARENT_ID = m2.MENU_ROUTE_ID "
					+ "where m1.MENU_URL = ? OR m1.MENU_URL = ?";

			result = (HashMap<String, Object>) db.querySingle(vaSql, vaMainPageName,poSrvlName);

			if (result != null) {
				return result;
			} else {
				return null;
			}
		} else {
			return result;
		}

	}
}